package cn.zcscloud.mall.controller;

import cn.zcscloud.mall.common.R;
import cn.zcscloud.mall.mbg.model.PmsSkuStock;
import cn.zcscloud.mall.service.PmsSkuStockService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

@CrossOrigin
@Slf4j
@RestController
@RequestMapping("/sku")
public class PmsSkuStockController {
    @Autowired
    private PmsSkuStockService pmsSkuStockService;

    @GetMapping("/{pid}")
    public R getSku(@PathVariable Integer pid){
        LambdaQueryWrapper<PmsSkuStock> lqw = new LambdaQueryWrapper<>();
        lqw.eq(PmsSkuStock::getProductId,pid);
        List<PmsSkuStock> list = pmsSkuStockService.list(lqw);
        log.info("byid == {}",list);
        return R.success(list);
    }

    @PostMapping("/update/{pid}")
    public R update(@RequestBody ArrayList<PmsSkuStock> pmsSkuStock,@PathVariable("pid") Integer pid){
        log.info("pmsSkuStockVo = {}",pmsSkuStock);
        log.info("pid = {}",pid);
        for (PmsSkuStock skuStock : pmsSkuStock) {
            LambdaQueryWrapper<PmsSkuStock> lqw = new LambdaQueryWrapper<>();
            lqw.eq(PmsSkuStock::getProductId,pid)
                    .eq(PmsSkuStock::getId,skuStock.getId());
            pmsSkuStockService.update(skuStock,lqw);
        }
        return R.success("");
    }
}
